package com.swfcamp.shopper.ebay
{
	import com.swfcamp.shopper.IProduct;
	import com.swfcamp.shopper.IProductProvider;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;

	public class EBayProduct implements IProduct
	{
		
		[Bindable]
		public var title:String;
		[Bindable]
		public var id:String;
		[Bindable]
		public var provider:IProductProvider;
		[Bindable]
		public var imageUrl:String;
		[Bindable]
		public var attributes:Dictionary;
		[Bindable]
		public var price:String;
		[Bindable]
		public var url:String;
		
		private var loader:URLLoader;
		
		private namespace EB = "urn:ebay:apis:eBLBaseComponents";
		
		public function EBayProduct(itemNumber:String,provider:EBayProductProvider)
		{
			this.id = itemNumber;
			this.provider = provider;
			
			loader = new URLLoader();
			var req:URLRequest = new URLRequest("http://open.api.ebay.com/shopping?callname=GetSingleItem&responseencoding=XML&appid="+provider.appId+"&siteid=0&version=515&ItemID=" + itemNumber);
			loader.addEventListener(Event.COMPLETE,productDetails,false,0,true);
			loader.load(req);
		}
	
		protected function productDetails(evt:Event):void{
			var xml:XML = XML(loader.data);
			title = String(xml.EB::Item.EB::Title);
			price = String(xml.EB::Item.EB::ConvertedCurrentPrice);
			url = String(xml.EB::Item.EB::ViewItemURLForNaturalSearch);
			imageUrl = String(xml.EB::Item.EB::GalleryURL);
		}
		
	}
}